package com.hushan.elevator.dao;

import com.hushan.elevator.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: zzy
 * @Date: 2024/10/22 23:38
 * @Title:
 * @Version:
 * @Package: com.hushan.elevator.dao
 */
@Repository
public interface RoleRepository extends JpaRepository<Role, Integer> {

    // 根据创建者查找角色
    List<Role> findByCreatedBy(Integer createdBy);

    // 根据是否删除查找角色
    List<Role> findByIsDeleted(Boolean deleted);

    // 根据创建时间范围查找角色
    List<Role> findByCreatedTimeBetween(LocalDateTime start, LocalDateTime end);

    // 根据更新时间范围查找角色
    List<Role> findByUpdatedTimeBetween(LocalDateTime start, LocalDateTime end);

    @Query(value = """
            SELECT   r.role_id,
                r.role_level,
                r.role_auth,
                r.role_title,
                r.created_by,
                r.created_time,
                r.updated_time,
                r.is_deleted FROM auth_role r \
            INNER JOIN auth_user_role ur ON r.role_id = ur.role_id \
            INNER JOIN auth_user u ON ur.user_id = u.user_id \
            WHERE u.user_id = :userId""", nativeQuery = true)
    Role findRoleByUser(Integer userId);

}